﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Public;
using BLL;

namespace QLNhanKhau
{
    public partial class frmTachKhau : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmTachKhau()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Thoát không lưu", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                this.MdiParent = null;
                this.Dispose();

            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            //luu so ho khau
            SoHoKhauPublic shk = new SoHoKhauPublic();
            shk.MaSHK = DungChung.LoadMaSHK();
            shk.SoSoHoKhau = int.Parse(txtSoSHKMoi.Text);
            shk.SoNha = txtSoNhaMoi.Text;
            shk.Duong = txtDuongMoi.Text;
            shk.KhuPho = byte.Parse(txtApMoi.Text);

            shk.NguoiCap = (int)cmbNguoiCapSHKMoi.SelectedValue;
            shk.CanBoDangKi = frmDangNhap.user_login.MaND;
            shk.NgayCap = dateNgayCap.Value;
            shk.ChucVuNguoiCap = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapSHKMoi.SelectedValue).MaCV;
            SoHoKhauBLL.InsertSoHoKhau(shk);
            //update mashk
            //NhanKhauBLL.UpdateMaSHK()

        }
        public int _MaSHKCu;
        private void btnTimSHKcu_Click(object sender, EventArgs e)
        {
            if (txtSoSHK.Text == "")
            {
                MessageBox.Show("Nhập số sổ hộ khẩu cần tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int SoSHK = int.Parse(txtSoSHK.Text);
            SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(SoSHK);
            if (shk == null)
            {
                txtDuong.Text = txtAp.Text = txtSoNha.Text = txtChuHo.Text = "";
                dgvNhanKhau.DataSource = null;
                groupBox1.Enabled = false;
                groupBox2.Enabled = false;
                MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                txtDuong.Text = shk.Duong;
                txtAp.Text = shk.KhuPho.ToString();
                txtSoNha.Text = shk.SoNha;

                List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadNhanKhauByMaSHK(shk.MaSHK);


                groupBox1.Enabled = true;
                groupBox2.Enabled = true;
                txtChuHo.Text = list_nk.Find(item => item.MaQH ==1).HoTen;
                //txtChuHo.Text = list_nk.Where(item => item.QuanHe == "Chủ hộ").FirstOrDefault().HoTen;

                dgvNhanKhau.DataSource = list_nk;
                //dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
                dgvNhanKhau.Columns[1].HeaderText = "Họ và tên";
                //dgvNhanKhau.Columns[3].HeaderText = "Tên gọi khác";
                dgvNhanKhau.Columns[4].HeaderText = "CMND";
                dgvNhanKhau.Columns[5].HeaderText = "Ngày sinh";
                //dgvNhanKhau.Columns[6].HeaderText = "Nơi sinh";
                dgvNhanKhau.Columns[7].HeaderText = "Giới tính";
                dgvNhanKhau.Columns[8].HeaderText = "Quê quán";
                dgvNhanKhau.Columns[9].HeaderText = "Dân tộc";
                dgvNhanKhau.Columns[10].HeaderText = "Nghề nghiệp";
                dgvNhanKhau.Columns[11].HeaderText = "Quốc tịch";
                //dgvNhanKhau.Columns[12].HeaderText = "Chứng tử";
                dgvNhanKhau.Columns[13].HeaderText = "Tôn giáo";
                dgvNhanKhau.Columns[14].HeaderText = "Trình độ";
                //dgvNhanKhau.Columns[15].HeaderText = "Sổ hộ khẩu";
                dgvNhanKhau.Columns[16].HeaderText = "Quan hệ";
                dgvNhanKhau.Columns[0].Visible = false;
                //dgvNhanKhau.Columns[1].Visible = false;
                dgvNhanKhau.Columns[2].Visible = false;
                dgvNhanKhau.Columns[3].Visible = false;
                //dgvNhanKhau.Columns[4].Visible = false;
                //dgvNhanKhau.Columns[5].Visible = false;
                dgvNhanKhau.Columns[6].Visible = false;
                //dgvNhanKhau.Columns[7].Visible = false;
                //dgvNhanKhau.Columns[8].Visible = false;
                dgvNhanKhau.Columns[9].Visible = false;
                //dgvNhanKhau.Columns[10].Visible = false;
                dgvNhanKhau.Columns[11].Visible = false;
                dgvNhanKhau.Columns[12].Visible = false;
                dgvNhanKhau.Columns[13].Visible = false;
                dgvNhanKhau.Columns[14].Visible = false;
                dgvNhanKhau.Columns[15].Visible = false;
                //dgvNhanKhau.Columns[16].Visible = false;

                cmbCanBoDangKiCu.Text = NguoiDungBLL.LoadNguoiDungByMaND(shk.CanBoDangKi).HoTen;
                cmbNguoiCapCu.Text = NguoiDungBLL.LoadNguoiDungByMaND(shk.NguoiCap).HoTen;
                dateNgayCapSHKCu.Value = (DateTime)shk.NgayCap;
                txtChucVuCu.Text=ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND(shk.NguoiCap).MaCV).TenChucVu;
                //cmbChucVuCu.Text=ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND(shk.NguoiCap).MaCV).TenChucVu;
                _MaSHKCu = shk.MaSHK;

            }
            
        }

        private void frmTachKhau_Load(object sender, EventArgs e)
        {
            txtSoSHKMoi.Text = DungChung.LoadMaSHK().ToString();
            //load cho nhân khẩu
            cmbNguoiCapNK.DataSource = NguoiDungBLL.LoadNguoiCap();
            cmbNguoiCapNK.DisplayMember = "HoTen";
            cmbNguoiCapNK.ValueMember = "MaND";
            //cmbChucVuNK.DataSource = ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapNK.SelectedValue).MaCV);
            //cmbChucVuNK.DisplayMember = "TenChucVu";
            //cmbChucVuNK.ValueMember = "MaCV";

            txtChucVuNK.Text = ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapNK.SelectedValue).MaCV).TenChucVu;
            dateNgayNhapNKMoi.Value = DateTime.Now;
            //txtNgayCapMoi.Text = DateTime.Now.Day.ToString();
            cmbCanBoDangKiNK.Text = frmDangNhap.user_login.HoTen;
            
            //load cho sổ hộ khẩu
            cmbCanBoDangKiSHKMoi.Text = frmDangNhap.user_login.HoTen;
            cmbNguoiCapSHKMoi.DataSource = NguoiDungBLL.LoadNguoiCap();
            cmbNguoiCapSHKMoi.DisplayMember = "HoTen";
            cmbNguoiCapSHKMoi.ValueMember = "MaND";
            //cmbChucVuMoi.DataSource = ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapSHKMoi.SelectedValue).MaCV);
            //cmbChucVuMoi.DisplayMember = "TenChucVu";
            //cmbChucVuMoi.ValueMember = "MaCV";
            //cmbChucVuMoi
            txtChucVuMoi.Text = ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapSHKMoi.SelectedValue).MaCV).TenChucVu;
            dateNgayCap.Value = DateTime.Now;

            //load số chuyển khẩu
            txtSoChuyenKhau.Text = DungChung.SoChuyenKhau().ToString();
            txtLyDoChuyen.Text = "Tách lập hộ mới";
            txtLyDoChuyen.Enabled = false;  
            txtNoiChuyen.Text = DungChung.LoadMaSHK().ToString();
            
        }

        private void txtSoSHKMoi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnChuyenDi_Click(object sender, EventArgs e)
        {
            if (txtApMoi.Text == "" || txtSoNhaMoi.Text == "" || txtDuongMoi.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (NhanKhauBLL.LoadNhanKhauByMaNK(_MaNKChuyen).MaQH == 1)
            {

                MessageBox.Show("Không thể chuyển chủ hộ", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            SoHoKhauPublic shk = new SoHoKhauPublic();
            shk.MaSHK = DungChung.LoadMaSHK();
            shk.SoSoHoKhau = int.Parse(txtSoSHKMoi.Text);
            shk.SoNha = txtSoNhaMoi.Text;
            shk.Duong = txtDuongMoi.Text;
            shk.KhuPho = byte.Parse(txtApMoi.Text);

            shk.NguoiCap = (int)cmbNguoiCapSHKMoi.SelectedValue;
            shk.CanBoDangKi = frmDangNhap.user_login.MaND;
            shk.NgayCap = dateNgayCap.Value;
            shk.ChucVuNguoiCap = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapSHKMoi.SelectedValue).MaCV;
            SoHoKhauBLL.InsertSoHoKhau(shk);

            NhanKhauBLL.UpdateMaSHK(shk.MaSHK, _MaNKChuyen);
            NhanKhauBLL.UpdateQuanHe(_MaNKChuyen, 1);

            ChuyenKhauPublic ck = new ChuyenKhauPublic();
            ck.MaNK = _MaNKChuyen;
            ck.MaCK = DungChung.LoadMaCDi();
            ck.SoChuyenKhau = DungChung.SoChuyenKhau();
            ck.LyDoChuyen = txtLyDoChuyen.Text;
            ck.NoiChuyen = shk.MaSHK.ToString();
            ck.NgayNhan = dateNgayCap.Value;
            ck.NgayCap = dateNgayCap.Value;
            ck.CanBoNoiChuyen = cmbCanBoDangKiSHKMoi.Text;
            ck.CanBoNoiNhan = cmbCanBoDangKiSHKMoi.Text;
            ck.NguoiCap = cmbNguoiCapSHKMoi.Text;
            ck.NguoiNhan = cmbNguoiCapSHKMoi.Text;
            ck.ChucVuNguoiChuyen = txtChucVuMoi.Text;
            ck.ChucVuNguoiNhan = txtChucVuMoi.Text;
            ck.NoiCap = int.Parse(ThietLapBLL.LoadMaDiaChi());
            ChuyenKhauBLL.InsertChuyenKhau(ck);
            MessageBox.Show("Tách khẩu thành công");
            dgvNhanKhau.DataSource = NhanKhauBLL.LoadNhanKhauByMaSHK(_MaSHKCu);
            dgvNhanKhauMoi.DataSource = NhanKhauBLL.LoadNhanKhauByMaSHK(shk.MaSHK);
            //dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
            dgvNhanKhau.Columns[1].HeaderText = "Họ và tên";
            //dgvNhanKhau.Columns[3].HeaderText = "Tên gọi khác";
            dgvNhanKhau.Columns[4].HeaderText = "CMND";
            dgvNhanKhau.Columns[5].HeaderText = "Ngày sinh";
            //dgvNhanKhau.Columns[6].HeaderText = "Nơi sinh";
            dgvNhanKhau.Columns[7].HeaderText = "Giới tính";
            dgvNhanKhau.Columns[8].HeaderText = "Quê quán";
            dgvNhanKhau.Columns[9].HeaderText = "Dân tộc";
            dgvNhanKhau.Columns[10].HeaderText = "Nghề nghiệp";
            dgvNhanKhau.Columns[11].HeaderText = "Quốc tịch";
            //dgvNhanKhau.Columns[12].HeaderText = "Chứng tử";
            dgvNhanKhau.Columns[13].HeaderText = "Tôn giáo";
            dgvNhanKhau.Columns[14].HeaderText = "Trình độ";
            //dgvNhanKhau.Columns[15].HeaderText = "Sổ hộ khẩu";
            dgvNhanKhau.Columns[16].HeaderText = "Quan hệ";
            dgvNhanKhau.Columns[0].Visible = false;
            //dgvNhanKhau.Columns[1].Visible = false;
            dgvNhanKhau.Columns[2].Visible = false;
            dgvNhanKhau.Columns[3].Visible = false;
            //dgvNhanKhau.Columns[4].Visible = false;
            //dgvNhanKhau.Columns[5].Visible = false;
            dgvNhanKhau.Columns[6].Visible = false;
            //dgvNhanKhau.Columns[7].Visible = false;
            //dgvNhanKhau.Columns[8].Visible = false;
            dgvNhanKhau.Columns[9].Visible = false;
            //dgvNhanKhau.Columns[10].Visible = false;
            dgvNhanKhau.Columns[11].Visible = false;
            dgvNhanKhau.Columns[12].Visible = false;
            dgvNhanKhau.Columns[13].Visible = false;
            dgvNhanKhau.Columns[14].Visible = false;
            dgvNhanKhau.Columns[15].Visible = false;
            //dgvNhanKhau.Columns[16].Visible = false;

        }

        
        private void cmbNguoiCapSHKMoi_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //cmbChucVuMoi.DataSource = ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapSHKMoi.SelectedValue).MaCV);
            //cmbChucVuMoi.DisplayMember = "TenChucVu";
            //cmbChucVuMoi.ValueMember = "MaCV";
            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapSHKMoi.SelectedValue);
            ////.Text = nd.ChucVu;
            txtChucVuMoi.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVuMoi.Enabled = false;
        }

        private void txtSoSHK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoSHK.Text.Length > 8)
            {
                e.Handled = true;
            }
        }

        private void txtSoChuyenKhau_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoSHK.Text.Length > 8)
            {
                e.Handled = true;
            }
        }
        public int _MaNKChuyen;
        private void dgvNhanKhau_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvNhanKhau.SelectedRows.Count > 0)
            {
                btnChuyenDi.Enabled = true;
                _MaNKChuyen = (int)dgvNhanKhau.SelectedRows[0].Cells[0].Value;
            }
            else
                btnChuyenDi.Enabled = false;
        }

        private void cmbNguoiCapNK_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //cmbChucVuNK.DataSource = ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapNK.SelectedValue).MaCV);
            //cmbChucVuNK.DisplayMember = "TenChucVu";
            //cmbChucVuNK.ValueMember = "MaCV";
            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapNK.SelectedValue);
            //.Text = nd.ChucVu;
            txtChucVuNK.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVuNK.Enabled = false;
        }

    }
}
